// treatment effects //

// training //

use "output/data.dta", clear
destring id, replace
xtset id time
foreach depvar in extension sprec rotation manage conserv machine pest l_rev l_exp {
	gen `depvar'_bl = L.`depvar'
}
replace yob = 1970 if yob==.

		
foreach depvar in extension sprec rotation manage conserv machine pest l_exp {
			
	foreach model in 1 2 3 {
	
		if `model' == 1 {
			loc spec reg `depvar' CAP if time==1 & CAP_ATEG==0 [w=wt], robust
		}
		else if `model' == 2 {
			loc spec reg `depvar' CAP yob sex res main coop area if time==1 & CAP_ATEG==0 [w=wt], robust
		}	
		else if `model' == 3 {
			loc spec reg `depvar' CAP yob sex res main coop area L.`depvar' if time==1 & CAP_ATEG==0 [w=wt], robust
		}	
		
		`spec'
		scalar b`model'`depvar' = _b[CAP]
		loc b`model'`depvar': display %4.3f b`model'`depvar'
		scalar se`model'`depvar' = _se[CAP]
		loc se`model'`depvar': display %4.3f se`model'`depvar'
		loc t = _b[CAP]/_se[CAP]
		loc p = 2*ttail(e(df_r),abs(`t'))
*		loc star`model'`depvar' = cond(`p'<0.01,"***",cond(`p'<0.05,"**",cond(`p'<0.10,"*","")))
		scalar n`model'`depvar' = e(N)
		loc obs`model'`depvar': display %3.0f n`model'`depvar' 
		
		scalar p`model'`depvar' = 2*ttail(e(df_r),abs(`t'))
		loc p`model'`depvar': display %4.3f p`model'`depvar'
				
	}
}

preserve

keep if time==1 & CAP_ATEG==0

rwolf extension sprec rotation manage conserv machine pest l_exp [w=wt], indepvar(CAP) reps(400) seed(1988)

foreach depvar in extension sprec rotation manage conserv machine pest l_exp {
	scalar rw1`depvar' = e(rw_`depvar')
	loc rw1`depvar': display %4.3f rw1`depvar'
}

rwolf extension sprec rotation manage conserv machine pest l_exp [w=wt], indepvar(CAP) controls(yob sex res main coop area ) reps(400) seed(1988)

foreach depvar in extension sprec rotation manage conserv machine pest l_exp {
	scalar rw2`depvar' = e(rw_`depvar')
	loc rw2`depvar': display %4.3f rw2`depvar'
}

rwolf extension sprec rotation manage conserv machine pest l_exp [w=wt], indepvar(CAP) controls(yob sex res main coop area) bl(_bl) reps(400) seed(1988)

foreach depvar in extension sprec rotation manage conserv machine pest l_exp {
	scalar rw3`depvar' = e(rw_`depvar')
	loc rw3`depvar': display %4.3f rw3`depvar'
}

restore

file open Table using "tables/tableS6.tex", write replace
		
file write Table "\begin{table}[h!]" _n 
file write Table "\centering" _n
file write Table "\small" _n
file write Table "\caption{Effects of Training}" _n
file write Table "\label{tab:effects1w}" _n
file write Table "\begin{tabularx} {\linewidth} {@{} l Z Z Z Z}" _n
file write Table "\toprule" _n
file write Table "\toprule" _n
file write Table " & (1) & (2) & (3) & N \\" _n		
file write Table "\midrule" _n
file write Table "Technical Assistance (0/1) & `b1extension'`star1extension' & `b2extension'`star2extension' & `b3extension'`star3extension' & `obs1extension' \\" _n
file write Table " & (`se1extension') & (`se2extension') & (`se3extension') & \\" _n
file write Table " & [`p1extension'] & [`p2extension'] & [`p3extension'] & \\" _n
file write Table " & \{`rw1extension'\} & \{`rw2extension'\} & \{`rw3extension'\} & \\" _n	
file write Table "Pasture Restoration (\%) & `b1sprec'`star2sprec' & `b2sprec'`star2sprec' & `b3sprec'`star3sprec' & `obs1sprec' \\" _n
file write Table " & (`se1sprec') & (`se2sprec') & (`se3sprec') & \\" _n
file write Table " & [`p1sprec'] & [`p2sprec'] & [`p3sprec'] & \\" _n
file write Table " & \{`rw1sprec'\} & \{`rw2sprec'\} & \{`rw3sprec'\} & \\" _n	
file write Table "Rotational Grazing (0/1) & `b1rotation'`star1rotation' & `b2rotation'`star2rotation' & `b3rotation'`star3rotation' & `obs1rotation' \\" _n
file write Table " & (`se1rotation') & (`se2rotation') & (`se3rotation')  & \\" _n
file write Table " & [`p1rotation'] & [`p2rotation'] & [`p3rotation'] & \\" _n	
file write Table " & \{`rw1rotation'\} & \{`rw2rotation'\} & \{`rw3rotation'\} & \\" _n	
file write Table "Management Practices & `b1manage'`star1manage' & `b2manage'`star2manage' & `b3manage'`star3manage' & `obs1manage' \\" _n
file write Table " & (`se1manage') & (`se2manage') & (`se3manage') & \\" _n	
file write Table " & [`p1manage'] & [`p2manage'] & [`p3manage'] & \\" _n
file write Table " & \{`rw1manage'\} & \{`rw2manage'\} & \{`rw3manage'\} & \\" _n	
file write Table "Conservation Practices & `b1conserv'`star1conserv' & `b2conserv'`star2conserv' & `b3conserv'`star3conserv' & `obs1conserv' \\" _n
file write Table " & (`se1conserv') & (`se2conserv') & (`se3conserv') & \\" _n	
file write Table " & [`p1conserv'] & [`p2conserv'] & [`p3conserv'] & \\" _n
file write Table " & \{`rw1conserv'\} & \{`rw2conserv'\} & \{`rw3conserv'\} & \\" _n	
file write Table "Machine Use (0/1) & `b1machine'`star1machine' & `b2machine'`star2machine' & `b3machine'`star3machine' & `obs1machine' \\" _n
file write Table " & (`se1machine') & (`se2machine') & (`se3machine')  & \\" _n
file write Table " & [`p1machine'] & [`p2machine'] & [`p3machine'] & \\" _n
file write Table " & \{`rw1machine'\} & \{`rw2machine'\} & \{`rw3machine'\} & \\" _n	
file write Table "Pesticide Use (0/1) & `b1pest'`star1pest' & `b2pest'`star2pest' & `b3pest'`star3pest' & `obs1pest' \\" _n
file write Table " & (`se1pest') & (`se2pest') & (`se3pest') & \\" _n		
file write Table " & [`p1pest'] & [`p2pest'] & [`p3pest'] & \\" _n
file write Table " & \{`rw1pest'\} & \{`rw2pest'\} & \{`rw3pest'\} & \\" _n			
file write Table "log(Expenditures) & `b1l_exp'`star1l_exp' & `b2l_exp'`star2l_exp' & `b3l_exp'`star3l_exp' & `obs1l_exp' \\" _n
file write Table " & (`se1l_exp') & (`se2l_exp') & (`se3l_exp') & \\" _n
file write Table " & [`p1l_exp'] & [`p2l_exp'] & [`p3l_exp'] & \\" _n
file write Table " & \{`rw1l_exp'\} & \{`rw2l_exp'\} & \{`rw3l_exp'\} & \\" _n	
file write Table "Controls & No & Yes & Yes & \\" _n  
file write Table "Dep. Var. in the Baseline & No & No & Yes & \\" _n  
file write Table "\bottomrule" _n
file write Table "\bottomrule" _n
file write Table "\end{tabularx}" _n
file write Table "\begin{tablenotes}" _n
file write Table "\small" _n
file write Table "\begin{spacing}{1.0}" _n
file write Table "\item Notes: {Each row reports the results for a different outcome. Column 1 reports the coefficient of a bivariate regression of each outcome on the dummy indicating that the farm was offered technical assistance. Column 2 adds the outcome itself measured in the baseline as a control. Column 3 adds the pre-determined characteristics as additional controls. The sample is restricted to farmers who completed the training. Robust standard errors are reported in parentheses. *** p<0.01; ** p<0.05; * p<0.10}" _n
file write Table "\end{spacing}" _n
file write Table "\end{tablenotes}" _n
file write Table "\end{table}%" _n
file close Table

// training plus technical assistance //
		
foreach depvar in extension sprec rotation manage conserv machine pest l_exp {
			
	foreach model in 1 2 3 {
	
		if `model' == 1 {
			loc spec reg `depvar' CAP_ATEG if time==1 & CTRL==0 [w=wt], robust
		}
		else if `model' == 2 {
			loc spec reg `depvar' CAP_ATEG yob sex res main coop area if time==1 & CTRL==0 [w=wt], robust
		}	
		else if `model' == 3 {
			loc spec reg `depvar' CAP_ATEG yob sex res main coop area L.`depvar' if time==1 & CTRL==0 [w=wt], robust
		}	
		
		`spec'
		scalar b`model'`depvar' = _b[CAP_ATEG]
		loc b`model'`depvar': display %4.3f b`model'`depvar'
		scalar se`model'`depvar' = _se[CAP_ATEG]
		loc se`model'`depvar': display %4.3f se`model'`depvar'
		loc t = _b[CAP_ATEG]/_se[CAP_ATEG]
		loc p = 2*ttail(e(df_r),abs(`t'))
*		loc star`model'`depvar' = cond(`p'<0.01,"***",cond(`p'<0.05,"**",cond(`p'<0.10,"*","")))
		scalar n`model'`depvar' = e(N)
		loc obs`model'`depvar': display %3.0f n`model'`depvar' 
		
		scalar p`model'`depvar' = 2*ttail(e(df_r),abs(`t'))
		loc p`model'`depvar': display %4.3f p`model'`depvar'
				
	}
}
		
preserve

keep if time==1 & CTRL==0

rwolf extension sprec rotation manage conserv machine pest l_exp [w=wt], indepvar(CAP_ATEG) reps(400) seed(1988)

foreach depvar in extension sprec rotation manage conserv machine pest l_exp {
	scalar rw1`depvar' = e(rw_`depvar')
	loc rw1`depvar': display %4.3f rw1`depvar'
}

rwolf extension sprec rotation manage conserv machine pest l_exp [w=wt], indepvar(CAP_ATEG) controls(yob sex res main coop area ) reps(400) seed(1988)

foreach depvar in extension sprec rotation manage conserv machine pest l_exp {
	scalar rw2`depvar' = e(rw_`depvar')
	loc rw2`depvar': display %4.3f rw2`depvar'
}

rwolf extension sprec rotation manage conserv machine pest l_exp [w=wt], indepvar(CAP_ATEG) controls(yob sex res main coop area) bl(_bl) reps(400) seed(1988)

foreach depvar in extension sprec rotation manage conserv machine pest l_rev l_exp {
	scalar rw3`depvar' = e(rw_`depvar')
	loc rw3`depvar': display %4.3f rw3`depvar'
}

restore
		
file open Table using "tables/tableS7.tex", write replace
		
file write Table "\begin{table}[h!]" _n 
file write Table "\centering" _n
file write Table "\small" _n
file write Table "\caption{Effects of Training and Technical Assistance}" _n
file write Table "\label{tab:effects2w}" _n
file write Table "\begin{tabularx} {\linewidth} {@{} l Z Z Z Z}" _n
file write Table "\toprule" _n
file write Table "\toprule" _n
file write Table " & (1) & (2) & (3) & N \\" _n		
file write Table "\midrule" _n
file write Table "Technical Assistance (0/1) & `b1extension'`star1extension' & `b2extension'`star2extension' & `b3extension'`star3extension' & `obs1extension' \\" _n
file write Table " & (`se1extension') & (`se2extension') & (`se3extension') & \\" _n
file write Table " & [`p1extension'] & [`p2extension'] & [`p3extension'] & \\" _n
file write Table " & \{`rw1extension'\} & \{`rw2extension'\} & \{`rw3extension'\} & \\" _n	
file write Table "Pasture Restoration (\%) & `b1sprec'`star2sprec' & `b2sprec'`star2sprec' & `b3sprec'`star3sprec' & `obs1sprec' \\" _n
file write Table " & (`se1sprec') & (`se2sprec') & (`se3sprec') & \\" _n
file write Table " & [`p1sprec'] & [`p2sprec'] & [`p3sprec'] & \\" _n
file write Table " & \{`rw1sprec'\} & \{`rw2sprec'\} & \{`rw3sprec'\} & \\" _n	
file write Table "Rotational Grazing (0/1) & `b1rotation'`star1rotation' & `b2rotation'`star2rotation' & `b3rotation'`star3rotation' & `obs1rotation' \\" _n
file write Table " & (`se1rotation') & (`se2rotation') & (`se3rotation')  & \\" _n
file write Table " & [`p1rotation'] & [`p2rotation'] & [`p3rotation'] & \\" _n	
file write Table " & \{`rw1rotation'\} & \{`rw2rotation'\} & \{`rw3rotation'\} & \\" _n	
file write Table "Management Practices & `b1manage'`star1manage' & `b2manage'`star2manage' & `b3manage'`star3manage' & `obs1manage' \\" _n
file write Table " & (`se1manage') & (`se2manage') & (`se3manage') & \\" _n	
file write Table " & [`p1manage'] & [`p2manage'] & [`p3manage'] & \\" _n
file write Table " & \{`rw1manage'\} & \{`rw2manage'\} & \{`rw3manage'\} & \\" _n	
file write Table "Conservation Practices & `b1conserv'`star1conserv' & `b2conserv'`star2conserv' & `b3conserv'`star3conserv' & `obs1conserv' \\" _n
file write Table " & (`se1conserv') & (`se2conserv') & (`se3conserv') & \\" _n	
file write Table " & [`p1conserv'] & [`p2conserv'] & [`p3conserv'] & \\" _n
file write Table " & \{`rw1conserv'\} & \{`rw2conserv'\} & \{`rw3conserv'\} & \\" _n	
file write Table "Machine Use (0/1) & `b1machine'`star1machine' & `b2machine'`star2machine' & `b3machine'`star3machine' & `obs1machine' \\" _n
file write Table " & (`se1machine') & (`se2machine') & (`se3machine')  & \\" _n
file write Table " & [`p1machine'] & [`p2machine'] & [`p3machine'] & \\" _n
file write Table " & \{`rw1machine'\} & \{`rw2machine'\} & \{`rw3machine'\} & \\" _n	
file write Table "Pesticide Use (0/1) & `b1pest'`star1pest' & `b2pest'`star2pest' & `b3pest'`star3pest' & `obs1pest' \\" _n
file write Table " & (`se1pest') & (`se2pest') & (`se3pest') & \\" _n		
file write Table " & [`p1pest'] & [`p2pest'] & [`p3pest'] & \\" _n
file write Table " & \{`rw1pest'\} & \{`rw2pest'\} & \{`rw3pest'\} & \\" _n			
file write Table "log(Expenditures) & `b1l_exp'`star1l_exp' & `b2l_exp'`star2l_exp' & `b3l_exp'`star3l_exp' & `obs1l_exp' \\" _n
file write Table " & (`se1l_exp') & (`se2l_exp') & (`se3l_exp') & \\" _n
file write Table " & [`p1l_exp'] & [`p2l_exp'] & [`p3l_exp'] & \\" _n
file write Table " & \{`rw1l_exp'\} & \{`rw2l_exp'\} & \{`rw3l_exp'\} & \\" _n	
file write Table "Controls & No & Yes & Yes & \\" _n  
file write Table "Dep. Var. in the Baseline & No & No & Yes & \\" _n  
file write Table "\bottomrule" _n
file write Table "\bottomrule" _n
file write Table "\end{tabularx}" _n
file write Table "\begin{tablenotes}" _n
file write Table "\small" _n
file write Table "\begin{spacing}{1.0}" _n
file write Table "\item Notes: {Each row reports the results for a different outcome. Column 1 reports the coefficient of a bivariate regression of each outcome on the dummy indicating that the farm was offered technical assistance. Column 2 adds the outcome itself measured in the baseline as a control. Column 3 adds the pre-determined characteristics as additional controls. The sample is restricted to farmers who completed the training. Robust standard errors are reported in parentheses. *** p<0.01; ** p<0.05; * p<0.10}" _n
file write Table "\end{spacing}" _n
file write Table "\end{tablenotes}" _n
file write Table "\end{table}%" _n
file close Table
